package com.htfyun.modem;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.htfyun.modem.databinding.ActivityModemUpdateBinding;
import com.htfyun.modem.utils.DebugLog;
import com.htfyun.modem.utils.SystemPropertyUtil;

import java.util.Objects;

public class ModemUpdateActivity extends AppCompatActivity {

    private final static String PROP_LOG_ENABLE = "sys.modem.log_enable";
    private final static String PROP_UPDATE_ENABLE = "sys.modem.update_enable";

    private final static String  PROP_UPDATE_STATUS = "sys.modem.update_status";
    private final static String PROP_VERSION_NEW = "persist.modem.version";
    private final static String PROP_VERSION_OLD = "gsm.version.baseband";

    private ActivityModemUpdateBinding binding;

    private final Handler handler = new Handler(Looper.getMainLooper());

    private final long DELAY = 1200L;
    private int progress = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityModemUpdateBinding.inflate(getLayoutInflater());

        setContentView(binding.getRoot());

        initListeners();

        binding.updateVersion.setText("模块版本号是: " + SystemPropertyUtil.get(PROP_VERSION_OLD));

    }

    private void initListeners() {
        binding.logStart.setOnClickListener(v -> SystemPropertyUtil.set(PROP_LOG_ENABLE, "1"));
        binding.logStop.setOnClickListener(v -> SystemPropertyUtil.set(PROP_LOG_ENABLE, "0"));

        binding.updateStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SystemPropertyUtil.set(PROP_UPDATE_ENABLE, "1");
                progress = 0;
                binding.updateProgress.setText("升级中: " + progress + " %");
                handler.postDelayed(updateProgressRunnable, DELAY);
            }
        });
    }

    private final Runnable updateProgressRunnable = new Runnable() {
        @Override
        public void run() {

            boolean keepPost = false;
            String status = SystemPropertyUtil.get(PROP_UPDATE_STATUS);

            DebugLog.e("status = " + status);

            UpdateStatus updateStatus = UpdateStatus.toUpdateStatus(status);

            switch (updateStatus) {
                case FAILED:
                    binding.updateProgress.setText("升级失败!");
                    break;
                case RUNNING:
                    keepPost = true;
                    progress += 5;
                    if (progress >= 100) {
                        progress = 95;
                    }
                    binding.updateProgress.setText("升级中: " + progress + " %");
                    break;
                case SUCCEEDED:
                    progress = 100;
                    binding.updateProgress.setText("升级成功! 新版本号是: " + SystemPropertyUtil.get(PROP_VERSION_NEW));
                    break;
                case CONNECTING:
                    keepPost = true;
                    binding.updateProgress.setText("升级中: " + progress + " %");
                    break;
                default:
                    keepPost = true;
                    break;
            }

            if (keepPost) {
                handler.postDelayed(this, DELAY);
            }
        }
    };

    private enum UpdateStatus {
        UNKNOWN("UNKNOWN"),
        CONNECTING("CONNECTING"),
        RUNNING("RUNNING"),
        FAILED("FAILED"),
        SUCCEEDED("SUCCEEDED"),
        ;
        final String status;

        UpdateStatus(String status) {
            this.status = status;
        }

        @NonNull
        public static UpdateStatus toUpdateStatus(String status) {
            for (UpdateStatus updateStatus : UpdateStatus.values()) {
                if (Objects.equals(status, updateStatus.status)) {
                    return updateStatus;
                }
            }

            return UNKNOWN;
        }
    }
}
